package com.levenx.aspect;

import com.levenx.web.annotation.ParamType;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

import static com.levenx.utils.ObjectTransformUtils.readValue;


//@Component
//@Aspect
public class CommonServiceAspect {


    @Pointcut("@annotation(com.levenx.web.annotation.ParamType)")
    public void paramTypePoint() {
    }

    @Around("paramTypePoint() && @annotation(type)")
    public Object around(ProceedingJoinPoint point, ParamType type) throws Throwable {
        ParamType annotation =
                point.getTarget().getClass().getAnnotation(ParamType.class);
        Class clazz = annotation.clazz();
        Object result = point.proceed(new Object[]{readValue(point.getArgs()[0],clazz)});
        return result;
    }


}
